{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "def approximate(d):\n",
    "    unit = 0.25\n",
    "    flag = True\n",
    "    if d < 0:\n",
    "        flag = False\n",
    "        d = -d\n",
    "    quotient = int(d / unit)\n",
    "    remainder = d % unit\n",
    "\n",
    "    if flag:\n",
    "        if remainder < unit / 2:\n",
    "            return unit * quotient\n",
    "        else:\n",
    "            return unit * (quotient + 1)\n",
    "    else:\n",
    "        if remainder < unit / 2:\n",
    "            return -(unit * quotient)\n",
    "        else:\n",
    "            return -(unit * (quotient + 1))\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "def pre_process(path):\n",
    "    data = np.load(path, allow_pickle=True)\n",
    "\n",
    "    data_T = data.T\n",
    "    data_T[3] = np.round(data_T[3])\n",
    "    data_T[5] = np.round(data_T[5])\n",
    "\n",
    "    for pitch in range(0, len(data_T[4])):\n",
    "        for sentence in range(0, len(data_T[4][pitch])):\n",
    "            data_T[4][pitch][sentence] = approximate(data_T[4][pitch][sentence])\n",
    "\n",
    "    data = data_T.T\n",
    "    return data"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "# def main():\n",
    "#     data = pre_process(\"epoch50_gen_data_5.npy\")\n",
    "#\n",
    "#\n",
    "# if __name__ == '__main__':\n",
    "#     main()\n",
    "data = pre_process(\"/home/b8313/coding/music/melody-generator-gan/src/save_/22_01_20/22_01_20_00_00_11/epoch40_gen_data_5.npy\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [
    "day='22_01_20'\n",
    "time='00_00_11'\n",
    "epoch=40\n",
    "base_data_url='/home/b8313/coding/music/melody-generator-gan/src/save_'\n",
    "total_size=6"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [],
   "source": [
    "url_list=[['{}/{}/{}_{}/epoch{}_gen_data_{}.npy'.format(base_data_url,day,day,time,epoch,i),0] for i in range(total_size)]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "77.0\n",
      "74.0\n",
      "75.0\n",
      "74.0\n",
      "72.0\n",
      "74.0\n",
      "74.0\n",
      "72.0\n",
      "67.0\n",
      "67.0\n",
      "67.0\n",
      "67.0\n",
      "72.0\n",
      "74.0\n",
      "72.0\n"
     ]
    }
   ],
   "source": [
    "for i in range(data.shape[1]):\n",
    "    print(data[0][i][0])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [],
   "source": [
    "def is_same(note1,note2)->bool:\n",
    "    if abs(note1-note2)<=3:\n",
    "        return True\n",
    "    else:\n",
    "        return False"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [],
   "source": [
    "def cal_pitch(data):\n",
    "    total_pitch=0\n",
    "    same_pitch=0\n",
    "    for j in range(data.shape[0]):\n",
    "        for i in range(data.shape[1]):\n",
    "            # print(data[j][i])\n",
    "            total_pitch+=1\n",
    "            if is_same(data[j][i][0],data[j][i][3]):\n",
    "                same_pitch+=1\n",
    "    return same_pitch/total_pitch\n",
    "# total_pitch,same_pitch,same_pitch/total_pitch"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0002\n"
     ]
    }
   ],
   "source": [
    "main_sum=0\n",
    "for item in url_list:\n",
    "    data=np.load(item[0],allow_pickle=True)\n",
    "    item[1]=cal_pitch(data)\n",
    "    main_sum+=item[1]\n",
    "\n",
    "print(main_sum/len(url_list))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}